package com.example.flower_shop.controller.usr;

import com.example.flower_shop.common.PageResult;
import com.example.flower_shop.service.usr.StoreCartService;
import com.example.flower_shop.vo.cart.CartPageReq;
import com.example.flower_shop.vo.cart.CartReq;
import com.example.flower_shop.vo.cart.CartResp;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

@Tag(name = "用户购物车模块", description = "用户购物车模块相关接口")
@RestController
@RequestMapping("/api/usr/cart")
public class CartController {

    @Resource
    private StoreCartService cartService;

    @GetMapping
    @Operation(summary = "获取购物车列表", description = "获取购物车列表")
    public PageResult<CartResp> getCart(@Valid CartPageReq req) {
        return cartService.getCart(req);

    }

    @PostMapping
    @Operation(summary = "添加或更新购物车商品", description = "添加或更新购物车商品")
    public void insertOrUpdateCart(@Valid CartReq req) {
        cartService.insertOrUpdateCart(req);
    }

    @PutMapping("/direct/update/{productId}")
    @Parameter(name = "productId", description = "购物车商品id", required = true, in = ParameterIn.PATH, example = "1")
    @Operation(summary = "直接修改购物车商品数量", description = "直接修改购物车商品数量")
    public void updateCartDirectly(@PathVariable Long productId, @RequestParam Integer num) {
        cartService.updateCartDirectly(productId, num);
    }

    @DeleteMapping
    @Operation(summary = "删除购物车商品", description = "删除购物车商品",
            parameters = {
                    @Parameter(name = "ids", description = "购物车商品id数组", required = true)
            })
    public void deleteCart(@RequestParam Long[] ids) {
        cartService.batchDelete(ids);
    }
}
